<?php

namespace app\common\model;


use think\Exception;

class MallGoodsSku extends TimeModel
{

    protected $name = "mall_goods_sku";


    public function getStatusList()
    {
        return ['0' => '禁用', '1' => '启用',];
    }

    /**
     * 根据商品id 获取所有规格
     * @param int $goods_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getGoodsSkuBySpecs(int $goods_id = 0): array
    {
        return $this->field('*,specs_value_ids as get_specs')
            ->where('goods_id', $goods_id)
            ->select()
            ->toArray();
    }

    /**
     * @param $value
     * @return string todo 颜色：红色
     */
    protected function getGetSpecsAttr($value): string
    {
        // 1-6
        $arr = explode('-', $value);
        if (empty($arr)) {
            return "";
        } else {
            $str = "";
            foreach ($arr as $v) {
                $field = "s.id as s_id,s.specs_id as s_specs_id,s.title as s_title,p.title as p_title,p.id as p_id";
                $ret = (new MallSpecsValue())->field($field)
                    ->alias('s')
                    ->join('mall_specs p', 's.specs_id = p.id')
                    ->where('s.id', $v)
                    ->find();
                if ($ret) {
                    $str .= $ret['p_title'] . ":" . $ret['s_title']."-";
                }
            }
            return rtrim($str,'-');
        }
    }
    public function getGoodsSkuBySpecsValue(int $id = 0){
        try {
            $row = $this->getBaseById($id);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        //获取规格
        if(empty($row) || empty($row['specs_value_ids'])){
            return "-";
        }
        $tags = explode('-',$row['specs_value_ids']);
        if (empty($tags)) {
            return "-";
        }
        try {
            $lists = (new MallSpecsValue())->getSpecsValueByIdValue($tags);
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        return $lists;

    }
}